package test0ne.多线程;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 流量控制
 */
public class SemaphoreTest {
    private static final int THREAD_COUNT = 30;
    private static Semaphore s = new Semaphore(10);
    private static volatile int xx = 1;

    private static ExecutorService e = Executors.newFixedThreadPool(THREAD_COUNT);

    public static void main(String[] args) {
        for (int i = 0; i < THREAD_COUNT; i++) {
            e.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        s.acquire();
                        System.out.println("可用的许可证"+s.availablePermits());
                        System.out.println("save data"+xx++);
                        s.release();
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }
        e.shutdown();
    }
}
